import mx.controls.ComboBox;

import com.yourdomain.spreadshop.gui.GUI;
import com.yourdomain.spreadshop.gui.mod.lib.Module;

import net.spreadshirt.logger.Logger;import net.spreadshirt.logger.TextAreaView;

/**
 * A developer tool class to display the log data.
 * Use the <D> key to show and hide the developer module.
 */
class com.yourdomain.spreadshop.gui.mod.DevTool {

	// The reference to the swf file.
	private static var _SWF:String = "spreadshop_devTool.swf";

	// Various visual settings.
	private static var _POSITION:Object = { x: 8, y: 8 };
	private static var _LABEL_TITLE:String = "Developer Console";

	// The reference to the GUI main class
	private var _gui:GUI;

	// The movie clip container
	private var _clip:MovieClip;

	// The log levels.	private var _logLevels:Array = [
		{ label: "Fatal",	data: 0 },		{ label: "Error",	data: 1 },		{ label: "Warn",	data: 2 },		{ label: "Info",	data: 3 },		{ label: "Debug",	data: 4 }
	];

	/**
	 * DevTool Constructor
	 */
	public function DevTool(
		gui:GUI,
		clip:MovieClip,
		level:Number
	) {
		// Connects with the GUI instance.
		_gui = gui;
		// Creates a new movie clip and set it's position on stage.
		_clip = clip.createEmptyMovieClip("devTool", level);
		// Creates a listener object to watch loading in progress.
		var mclListener:Object = new Object();
		mclListener.timer = [0, 0];
		mclListener.onLoadStart = function(target:MovieClip):Void {
		    this.timer[0] = getTimer();
		    Logger.getLog().info("GUI - DevTool: start loading");
		};
		mclListener.onLoadComplete = function(target:MovieClip):Void {
			this.timer[1] = getTimer();
		};
		mclListener.devtool = this;
		mclListener.onLoadInit = function(target:MovieClip):Void {
			var timer:Number = this.timer[1] - this.timer[0];
			var bytes:Number = target.getBytesLoaded();
			Logger.getLog().info("GUI - ShoppingCart: "
				+ bytes +" Bytes loaded in "+ timer +" ms.");
			this.devtool.setup();
		};
		// Creates movie clip loader an attachs the listener object.
		var mcLoader:MovieClipLoader = new MovieClipLoader();
		mcLoader.addListener(mclListener);
		mcLoader.loadClip(_gui.swf_path + _SWF, _clip);
	}

	/**
	 * Setup
	 */
	private function setup():Void {
		_gui.initModuleDone("initDevTool");
		// Initializing the console.
		initLoggerView();
		initLoggerLevelComboBox();
		initClear();
		initReset();
		// Creates an alias to the developer tool movie clip.
		var clip:MovieClip = _clip.devTool;
		// Setup modul.
		var module:Module = new Module(
			_gui,
			clip,
			false,
			_LABEL_TITLE,
			_POSITION.x,
			_POSITION.y
		);
		// Create HotKey listener.
		clip.onKeyDown = function():Void {
			if(Key.getCode() == 68) {
				// <D> is pressed
				this[(this._visible) ? "hide" : "show"]();
			}
		};
		Key.addListener(clip);
	}

	/**
	 * Initialize Logger View
	 */	private function initLoggerView():Void {		// Creates an alias to the developer tool movie clip.		var devtool:MovieClip = _clip.devTool;
		// Setup the text area.
		var textArea:Object = devtool.log;
		textArea.editable = false;
		textArea.setStyle("backgroundColor", "0xeeeeee");
		textArea.setStyle("color", "0x666666");
		// Creates an alias to the temporary log view.
		var tmpLogView:Object = _clip._parent._parent.tfv;
		// Copys temporary text to the debug view text area.
		textArea.text = tmpLogView.out.text;
		// Removes the temporary log view.
		tmpLogView.destroy();		// Creates log and observer.		var log:Logger = Logger.getLog();		var textLogView:TextAreaView;		textLogView = new TextAreaView(log, devtool.log);		log.addObserver(textLogView);	}

	/**
	 * Setup Logger level combo box
	 */
	private function initLoggerLevelComboBox():Void {
		// Creates an alias to the developer tool movie clip.
		var devtool:MovieClip = _clip.devTool;
		// Setup the logger level combobox.
		devtool._lockroot = true;		var comboBox:ComboBox = devtool.loglevel;
		comboBox.tabEnabled = false;		comboBox.dataProvider = _logLevels;
		comboBox.rowCount = _logLevels.length;
		comboBox.selectedIndex = Logger.getLog().getLevel();
		comboBox.setStyle("color", "0x0099ff");		var comboBoxListener:Object = new Object();
		comboBoxListener.change = function(evt:Object):Void {
			var level:Number = evt.target.selectedItem.data;
			Logger.getLog().setLevel(level);
		};
		comboBox.addEventListener("change", comboBoxListener);
	}

	/**
	 * Setup clear button
	 */
	private function initClear():Void {
		// Creates an alias to the developer tool movie clip.
		var btclear:MovieClip = _clip.devTool.btclear;
		btclear.tabEnabled = false;		btclear.onPress = function():Void {
			this._parent.log.text = "";
		};
		_clip.devTool.shop_id.text = String(_gui.getShopID());
	}

	/**
	 * Setup reset button
	 */
	private function initReset():Void {
		// Creates an alias to the developer tool movie clip.
		var btreset:MovieClip = _clip.devTool.btreset;
		btreset.tabEnabled = false;		btreset.initShop = initShop;		btreset.onPress = function():Void {
			var shop_id:Number = parseInt(this._parent.shop_id.text);
			this.initShop(shop_id);
		};
		_clip.devTool.shop_id.text = String(_gui.getShopID());
	}

	/**
	 * Re-Initializes the shop.
	 */
	public function initShop(shop_id:Number):Void {
		Logger.getLog().info("REINIT SHOP [ID "+ shop_id +"]");
		net.spreadshirt.Spreadshop.reinit(shop_id);
	}

}
